#coding:utf8
'''
Created on 2017/2/22
15:20
@author: Moon
qq : 550302989
'''

import datetime
import chardet
import Feature

class TaskManage(object):

    def __init__(self):
        # self.__Feature = __import__("Feature")
        # self.__feature = getattr(self.__Feature,"Feature")()
        self.__feature = Feature.Feature()

    # 创建发布任务（管理员）
    def addTask(self,task_info):
        sql = "insert into task(tasktitle,cmanid,taskcontent,status,speed,createtime) " \
              "values('{tasktitle}','{cmanid}','{taskcontent}','{status}','{speed}','{createtime}')".format(
            tasktitle=task_info['tasktitle'],cmanid=task_info['cmanid'],taskcontent=task_info['taskcontent'],status=0,speed=0,createtime=datetime.datetime.now().strftime('%Y-%m-%d')
        )
        return self.__feature.dataCommitFeature(sql)

    # 查看所有未有人接的任务 的所有信息
    def findTask(self):
        sql = "SELECT task.id,tasktitle,users.name,taskcontent,status,mamberid,speed,createtime FROM task,users WHERE task.cmanid = users.id and status=0"
        return self.__feature.findFeatureAll(sql)

    # 根据任务id查看任务状态
    def findTask(self,taskid):
        sql = "select status from task where id='{taskid}'".format(taskid=taskid)
        return self.__feature.findFeatureOne(sql)

    # 修改任务标题与内容（任务发布者）
    def updateTask(self,taskid,tasktitle,taskcontent):
        sql = "update task set tasktitle='{tasktitle}',taskcontent='{taskcontent}' where id='{taskid}'".format(
            tasktitle = tasktitle,taskcontent = taskcontent,taskid = taskid
        )
        return self.__feature.dataCommitFeature(sql)

    # 删除任务（至高权限）
    def deleteTask(self,taskid):
        sql = "delete from task where id='{taskid}'".format(taskid=taskid)
        return self.__feature.dataCommitFeature(sql)

    # 显示用户进行中的任务
    def findUserProgressTask(self,mamberid):
        sql = "select id,tasktitle,cmanid,taskcontent,status,mamberid,speed,createtime from task where mamberid='{mamberid}'".format(mamberid=mamberid)
        return self.__feature.findFeatureAll(sql)
